package com.etc.wordcount;



import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 上等的猪头肉
 * @Title: CountBolt
 * @ProjectName storm
 * @Description: TODO
 * @date 2018/11/15 16:02
 */
public class CountBolt extends BaseRichBolt {


    Map<String, Integer> dbmap = new HashMap<String, Integer>();


    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {


    }

    @Override
    public void execute(Tuple input) {
        String word = input.getString(0);
        int ct = 1;
        if(dbmap.containsKey(word)) {
            ct = dbmap.get(word).intValue() + 1;
        }

        System.err.println("##CountBolt  Word: " + word + "   Count: " + ct);
        dbmap.put(word, ct);
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {

    }
}
